package cn.s1995.sloth.bms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.s1995.sloth.bizs.entity.Demo;
import cn.s1995.sloth.bizs.service.DemoService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * demo
 *
 * @author Lex
 * @date 2023-12-22 18:27:04
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/demo")
@Tag(description = "Demo", name = "BMS-demo管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DemoController {

    private final DemoService demoService;

    /**
     * 获取分页
     * @param page 分页对象
     * @param req demo
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取分页", description = "获取分页")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_Demo_view')")
    public R getPage(@ParameterObject Page page, @ParameterObject Demo req) {
        LambdaQueryWrapper<Demo> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(StrUtil.isNotBlank(req.getUsername()), Demo::getUsername, req.getUsername());
		wrapper.eq(Objects.nonNull(req.getState()), Demo::getState, req.getState());
		wrapper.eq(Objects.nonNull(req.getGender()), Demo::getGender, req.getGender());
		wrapper.eq(StrUtil.isNotBlank(req.getHobby()), Demo::getHobby, req.getHobby());
		wrapper.eq(StrUtil.isNotBlank(req.getContent()), Demo::getContent, req.getContent());
        return R.ok(demoService.page(page, wrapper));
    }

    /**
     * 获取列表
     * @param req demo
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取列表", description = "获取列表")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('bizs_Demo_view')")
    public R getList(@ParameterObject Demo req) {
        LambdaQueryWrapper<Demo> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(StrUtil.isNotBlank(req.getUsername()), Demo::getUsername, req.getUsername());
		wrapper.eq(Objects.nonNull(req.getState()), Demo::getState, req.getState());
		wrapper.eq(Objects.nonNull(req.getGender()), Demo::getGender, req.getGender());
		wrapper.eq(StrUtil.isNotBlank(req.getHobby()), Demo::getHobby, req.getHobby());
		wrapper.eq(StrUtil.isNotBlank(req.getContent()), Demo::getContent, req.getContent());
        return R.ok(demoService.list(wrapper));
    }

    /**
     * 获取对象
     * @param id id
     * @return R
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取对象", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_Demo_view')")
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(demoService.getById(id));
    }

    /**
     * 新增demo
     * @param req demo
     * @return R
     */
	@ApiOperationSupport(order = 10)
    @Operation(summary = "新增demo", description = "新增demo")
    @SysLog("新增demo")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_Demo_add')")
    public R save(@RequestBody Demo req) {
        return R.ok(demoService.save(req));
    }

    /**
     * 修改demo
     * @param req demo
     * @return R
     */
	@ApiOperationSupport(order = 20)
    @Operation(summary = "修改demo", description = "修改demo")
    @SysLog("修改demo")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_Demo_edit')")
    public R updateById(@RequestBody Demo req) {
        return R.ok(demoService.updateById(req));
    }

    /**
     * 通过id删除demo
     * @param ids id列表
     * @return R
     */
	@ApiOperationSupport(order = 30)
    @Operation(summary = "通过id删除demo", description = "通过id删除demo")
    @SysLog("通过id删除demo")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_Demo_del')")
    public R remove(@RequestBody Long[] ids) {
        return R.ok(demoService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
	@ApiOperationSupport(order = 40)
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_Demo_export')")
    public List<Demo> export(Demo req, Long[] ids) {
        return demoService.list(Wrappers.lambdaQuery(req).in(ArrayUtil.isNotEmpty(ids), Demo::getId, ids));
    }
}
